package com.lzk.auth.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lzk.auth.mapper.OrderMapper;
import com.lzk.auth.mapper.PlanMapper;
import com.lzk.auth.service.PlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzk.model.system.SysOrder;
import com.lzk.model.system.SysPlan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lzk
 * @since 2024-01-02
 */
@Service
public class PlanServiceImpl extends ServiceImpl<PlanMapper, SysPlan> implements PlanService {
    @Autowired
    private OrderMapper orderMapper;
    //统计销量
    @Override
    public List<Integer> salary() {

        List<Integer> monthlySales = new ArrayList<>(12); // 创建一个列表用于存放每个月的销售数量

        for (int i = 0; i < 12; i++) {
            monthlySales.add(0); // 初始化每个月销售数量为0
        }
        LambdaQueryWrapper<SysPlan> wrapper = new LambdaQueryWrapper<>();
        wrapper.le(SysPlan::getEndTime, LocalDateTime.now());
        List<SysPlan> sysPlans = baseMapper.selectList(wrapper);
        for (SysPlan sysPlan : sysPlans) {
            LocalDateTime endTime = sysPlan.getEndTime();
            int month = endTime.getMonthValue();
            int year = endTime.getYear();
            if (month >= 1 && month <= 12 && year==2023) {
                Long orderId = sysPlan.getOrderId();
                SysOrder sysOrder = orderMapper.selectById(orderId);
                if (sysOrder != null) {
                    String pCount = sysOrder.getPCount();
                    String[] counts = pCount.split(",");
                    int total=0;
                    for (String count : counts) {
                        Integer countNumber=Integer.parseInt(String.valueOf(count));
                        total+=countNumber;

                    }

                    monthlySales.set(month-1,monthlySales.get(month-1)+total);

                }
            }


        }
        return monthlySales;
    }
}
